package com.ruoyi.web.controller.login;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthGiteeRequest;
import org.apache.tomcat.util.bcel.classfile.Constant;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class GiteeLoginController {
    @GetMapping("/ProLoginByGitee")
    public AjaxResult ProLoginByGitee(){
        AjaxResult ajax = AjaxResult.success();
        AuthGiteeRequest authGiteeRequest = new AuthGiteeRequest(AuthConfig.builder()
                .clientId("9715e55a9fc7da58ab69830064c7fac3724f0571a6863fa99047263ed2956156")
                .clientSecret("e3f3a5440a5abfd354b1c0fe6ec746428d43459866bfb6277a7283e19323d361")
                .redirectUri("http://localhost/callback")
                .build());
        String uuid = IdUtils.fastUUID();
        String authorizeUrl = authGiteeRequest.authorize(uuid);
        ajax.put("authorizeUrl", authorizeUrl);
        ajax.put("uuid", uuid);
        return ajax;

    }
    @Resource
    private SysLoginService loginService;
    @PostMapping("/loginByGitee")
    public AjaxResult loginByGitee(@RequestBody LoginByOtherSourceBody loginByOtherSourceBody){
        AjaxResult ajax = AjaxResult.success();
        String token =  loginService
                .loginByOtherSource(loginByOtherSourceBody.getCode(), loginByOtherSourceBody.getSource(), loginByOtherSourceBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
}
